
function JntsDialogPositionCookieStorage(dialogSelector,cookiePrefix,cookieExpire)
{
	// Load dialog positions from cookie
	$(dialogSelector).each( 
		function( index ) {
			if ($.cookie(cookiePrefix + this.id + "_width") != null )
			{
				$(this).dialog( "option", "width", parseInt($.cookie(cookiePrefix + this.id + "_width")) );
				$(this).dialog( "option", "height", parseInt($.cookie(cookiePrefix + this.id + "_height")) );
				$(this).dialog( "option", "position", [parseInt($.cookie(cookiePrefix + this.id + "_left")),parseInt($.cookie(cookiePrefix + this.id + "_top"))] );
			}
		});
	// dialogdragstop
	$(dialogSelector).bind('dialogdragstop', 
		function ( event, ui )
		{	
			$.cookie(cookiePrefix + this.id + "_left", $(this).dialog( "option", "position" )[0], {expires: cookieExpire});
			$.cookie(cookiePrefix + this.id + "_top", $(this).dialog( "option", "position" )[1], {expires: cookieExpire});
		});
	// dialogresizestop
	$(dialogSelector).bind('dialogresizestop', 
		function ( event, ui )
		{	
			$.cookie(cookiePrefix + this.id + "_width", Math.round($(this).dialog("option","width")), {expires: cookieExpire});
			$.cookie(cookiePrefix + this.id + "_height", Math.round($(this).dialog("option","height")), {expires: cookieExpire});
		});
}

function JntsPercentile(p,sortedValues)
{
	var n = sortedValues.length;
	if (n == 1)
		return sortedValues[0];
	var pos = (p+0.0) * (n + 1.0) / 100.0;
	var intPos = Math.floor(pos);
	var dif = pos - intPos;
	if (pos < 1) {
		return sortedValues[0];
	}
	if (pos >= n) {
		return sortedValues[sortedValues.length - 1];
	}
	var lower = sortedValues[intPos - 1];
	var upper = sortedValues[intPos];
	return (lower + dif * (upper - lower));
}

function RGBToHex(rgb) 
{
	//return '#'+rgb[0].toString(16)+rgb[1].toString(16)+rgb[2].toString(16);
	var rgb = rgb[2] | (rgb[1] << 8) | (rgb[0] << 16);
	var result = rgb.toString(16);
	while (result.length < 6)
		result = '0'+result;
	return '#'+result;
}

function HexToRGB(hex) 
{
  var color = [];
  color[0] = parseInt(hex.substring(1,3), 16);
  color[1] = parseInt(hex.substring(3,5), 16);
  color[2] = parseInt(hex.substring(5,7), 16);
  return color;
}

var colorCache = [];

//initColorCache('#FF0000','#FFFF00','#00FF00');

function initColorCache(startColorHex,midColorHex,endColorHex)
{
	/*var start = HexToRGB(startColorHex);
	var end = HexToRGB(midColorHex);
	for (var i=0;i<=50;++i)
	{
		var t = (i+0.0)/50.0;
		var c = [];
		c[0] = Math.round(end[0]*t + (1-t)*start[0]);
		c[1] = Math.round(end[1]*t + (1-t)*start[1]);
		c[2] = Math.round(end[2]*t + (1-t)*start[2]);
		colorCache[i] = RGBToHex(c);
	}
	var start = HexToRGB(midColorHex);
	var end = HexToRGB(endColorHex);
	for (var i=1;i<=50;++i)
	{
		var t = (i+0.0)/50.0;
		var c = [];
		c[0] = Math.round(end[0]*t + (1-t)*start[0]);
		c[1] = Math.round(end[1]*t + (1-t)*start[1]);
		c[2] = Math.round(end[2]*t + (1-t)*start[2]);
		colorCache[i+50] = RGBToHex(c);
	}*/
	var start = HexToRGB(startColorHex);
	var end = HexToRGB(endColorHex);
	for (var i=0;i<=100;++i)
	{
		var t = (i+0.0)/100.0;
		var c = [];
		c[0] = Math.round(end[0]*t + (1-t)*start[0]);
		c[1] = Math.round(end[1]*t + (1-t)*start[1]);
		c[2] = Math.round(end[2]*t + (1-t)*start[2]);
		colorCache[i] = RGBToHex(c);
	}
}

function getValueColor(value)
{
	return colorCache[value];
}

function getGradientStr(lowValue,highValue)
{
	var startColor = getValueColor(Math.min(lowValue,highValue));
	var endColor = getValueColor(Math.max(lowValue,highValue));
	/*var midColor = getValueColor(Math.round((lowValue+highValue)/2));
	return 'background: '+startColor+';'
		+'background: -moz-linear-gradient(bottom, '+startColor+' 0%,'+midColor+' 50%,'+endColor+' 100%);'
		+'background: -webkit-gradient(linear, left bottom, left top, from('+startColor+'), to('+endColor+'), color-stop(.5,'+midColor+'));';*/
	return 'background: '+startColor+';'
		+'background: -moz-linear-gradient(bottom, '+startColor+','+endColor+');'
		+'background: -webkit-gradient(linear, left bottom, left top, from('+startColor+'), to('+endColor+'));';
}

function timeToStr(date)
{
	var result = '';
	var hours = date.getHours();
	if (hours < 10) result += '0';
	result += hours+':';
	var mins = date.getMinutes();
	if (mins < 10) result += '0';
	result += mins;
	return result;
}

var JSON = JSON || {};

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) 
{
	var t = typeof (obj);
	if (t != "object" || obj === null) {
		// simple data type
		if (t == "string") obj = '"'+obj+'"';
		return String(obj);
	}
	else {
		// recurse array or object
		var n, v, json = [], arr = (obj && obj.constructor == Array);
		for (n in obj) {
			v = obj[n]; t = typeof(v);
			if (t == "string") v = '"'+v+'"';
			else if (t == "object" && v !== null) v = JSON.stringify(v);
			json.push((arr ? "" : '"' + n + '":') + String(v));
		}
		return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	}
};
